/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2022-12-01
 * Time: 22:42
 */
// 1、无头单向非循环链表实现
public class SingleLinkedList {
    static class ListNode {
        public int val;//数值域public ListNode next;//存储下一个节点的地址
        public ListNode(int val) {
            this.val = val;
        }
        ListNode next;
    }
    public ListNode head;//代表单链表的头结点的引用
    /**
     * 这里只是简单的进行，链表的构造。
     */public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;

        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }


    //头插法
    public void addFirst(int data){
        ListNode newHead = new ListNode(data);
         if (this.head == null){
             head = newHead;
             return;
         }
        newHead.next = this.head;
        this.head = newHead;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    public void checkIndex(int index) throws MySingleListIndexOutOfException{
         if (index < 0 || index >= this.size()){
             throw new MySingleListIndexOutOfException("该index下标不合法！");
         }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == this.size() - 1){
            addLast(data);
            return;
        }
        ListNode cur = this.head;
        ListNode prev = null;
        int count = 0;
        while (count != index){
            prev = cur;
            cur = cur.next;
            count++;
        }
        ListNode node = new ListNode(data);
        node.next = prev.next;
        prev.next = node;


    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
         if (this.head.val == key){
             this.head = this.head.next;
             return;
         }
        ListNode cur = this.head;
        ListNode prev = null;
        while (cur.val != key){
            prev = cur;
            cur = cur.next;
        }
        prev.next = cur.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
         ListNode dummyHead = new ListNode(0);
         dummyHead.next = this.head;
         ListNode cur = dummyHead;
         while (cur.next != null){
             if (cur.next.val == key){
                 cur.next = cur.next.next;
             }else {
                 cur = cur.next;
             }
         }

    }
    //得到单链表的长度
    public int size(){
        ListNode node = this.head;
        int count = 0;
        while (node != null){
            node = node.next;
            count++;
        }
        return count;
    }
    public void display(){
         ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        this.head = null;
    }
}
